package com.fxkj.cloud.drms.ui.mqtt.handler.receive;

import com.fxkj.cloud.dbs.common.constants.DeviceStatusEnum;
import com.fxkj.cloud.drms.application.DeviceApplicationService;
import com.fxkj.cloud.drms.infra.mq.mqtt.MqttConstants;
import com.fxkj.cloud.drms.ui.mqtt.handler.AbstractReceiveHandler;
import com.fxkj.cloud.drms.ui.mqtt.model.MqttClientOfflineVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * <p>description: </p>
 * @author ww
 * @date 2025/3/17 13:43
 */
@Component(MqttConstants.Topic.System.SYSTEM_DEVICE_OFFLINE_SERVICE_NAME)
@Slf4j
public class SystemDeviceOfflineHandler extends AbstractReceiveHandler<MqttClientOfflineVO> {
    private final DeviceApplicationService deviceApplicationService;

    public SystemDeviceOfflineHandler(DeviceApplicationService deviceApplicationService) {
        this.deviceApplicationService = deviceApplicationService;
    }

    @Override
    protected void doHandler(String topic, MqttClientOfflineVO data, String... topicParams) throws Exception {
        log.info("[SystemDeviceOfflineHandler] -| 设备下线 serialNo: [{}], topic: {}, data: {}", topicParams, topic, data);
        String serialNo = topicParams[1];
        deviceApplicationService.deviceStatusChange(serialNo, DeviceStatusEnum.OFFLINE);
    }

    @Override
    protected Class<MqttClientOfflineVO> getClazz() {
        return MqttClientOfflineVO.class;
    }

    @Override
    protected int indexNum() {
        return 2;
    }
}
